package com.hg.chapter3.aware;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

@Service
public class AwareService implements BeanNameAware, ResourceLoaderAware, ApplicationContextAware {

    private String bean;
    private ResourceLoader loader;
    private ApplicationContext context;

    @Override
    public void setBeanName(String s) {
        this.bean = s;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.loader = resourceLoader;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    public void output() {
        System.out.println("Bean的名称" + this.bean);
        //指的是target
        Resource resource = loader.getResource("classpath:com/hg/chapter3/aware/test.txt");

        try {
            System.out.println("resource加载出" + IOUtils.toString(resource.getInputStream()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
